local handler = require "handler"
local util = require "util"
local reason = require "reason"
local error_code = require "error_code"
local constant = require "constant"
local cocall = require "cocall"
local game_def = require "game_def"

local table_insert = table.insert
local table_sort = table.sort
local math_floor = math.floor
local notify_money_changed = handler.notify_money_changed
local add_awards = handler.add_awards
local tostring = tostring

local activity_handler = {}

function activity_handler.REQ_ACTIVITY_LIST(msg)
    local uid = player.uid
    local ok,ret = R().activitysvr(1):call('.manager','get_activity_list',uid)
    if not ok or not ret then
        send_to_gateway('activity.RSP_ACTIVITY_LIST',{result = error_code.GET_ACTIVITY_LIST_ERROR})
        return
    end
    send_to_gateway('activity.RSP_ACTIVITY_LIST',ret)

    return true
end

local function make_rsp_first_charge_award(uid,act_id,ret_data,activity)
    if ret_data.charge_state ~= 1 then
        send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',{result = -2})
        return
    end

    if ret_data.taken_status == 1 then
        send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',{result = -3})
        return
    end

    if ret_data.cur_process < activity.ac_content.earn_coins then
        send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',{result = -4})
        return
    end


end

function activity_handler.REQ_ACTIVITY_RECEIVE_AWARD(msg)
    local uid = player.uid
    local act_id = msg.act_id
    local task_type = msg.task_type
    local task_index = msg.task_index

    local activity = global_configs.activity[act_id]
    if activity and activity.ac_type == game_def.ACTIVITY_TYPE_FIRST_CAHRGE then
        --领取充值奖励
        local ok,ret =  R().activitysvr(1):call('.manager','take_task_act_award',uid,act_id,task_type,activity.ac_content.earn_coins)
        print("take_task_act_data=============",ok,ret)
        if ok and ret then --加钱
            local exinfo = {channel = player.channel,cur_channel = player.cur_channel}
            local ok,succ,ret = R().basesvr({key=uid}):call('.msg_handler','add_coins',uid,activity.ac_content.back_coins,reason.TASK_ACT,exinfo)
            if ok and succ and ret then
                notify_money_changed(uid,{coins = ret.curr})
            end
        else
            return send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',{result = -2})
        end

        local rsp = {act_id = msg.act_id,task_type = msg.task_type,task_index = msg.task_index}
        send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',rsp)
        return true
    end

    local ok,ret_data = R().activitysvr(1):call('.manager','get_task_act_data',uid,act_id)
    if not ok then
        send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',{result = -1})
        return
    end

    local task_list = {}
    for _,task in pairs(ret_data) do
        if task.task_type == task_type then
            table_insert(task_list,task)
        end
    end
    local task = task_list[task_index]
    if not task then
        send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',{result = -2})
        return
    end
    print("REQ_ACTIVITY_RECEIVE_AWARD============",tostring_r(task))

    if task.bt_status ~= 1 then --不可以领取
        send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',{result = -3})
        return
    end

    print("REQ_ACTIVITY_RECEIVE_AWARD===============",act_id,task_type,task.target_process)
    local ok,ret =  R().activitysvr(1):call('.manager','take_task_act_award',uid,act_id,task_type,task.target_process)
    print("take_task_act_data=============",ok,ret)
    if ok and ret then --加钱
        local exinfo = {channel = player.channel,cur_channel = player.cur_channel}
        local ok,succ,ret = R().basesvr({key=uid}):call('.msg_handler','add_coins',uid,task.award_coins,reason.TASK_ACT,exinfo)
        if ok and succ and ret then
            notify_money_changed(uid,{coins = ret.curr})
        end
    end

    local rsp = {act_id = msg.act_id,task_type = msg.task_type,task_index = msg.task_index}
    send_to_gateway('activity.RSP_ACTIVITY_RECEIVE_AWARD',rsp)
    return true
end

function activity_handler.REQ_ACTIVITY_RANK(msg)
    local rank_type = msg.rank_type
    local ok,ret = R().activitysvr(1):call('.manager','act_get_rank_list',rank_type)
    print("REQ_ACTIVITY_RANK====================",ok,ret)
    if not ok or not ret then
        send_to_gateway('activity.RSP_ACTIVITY_RANK',{result = error_code.RSP_FAIL})
        return
    end
    send_to_gateway('activity.RSP_ACTIVITY_RANK',{rank_list = ret})

    return true
end

--请求红包奖励
function activity_handler.REQ_REDPACK_AWARD(msg)
    msg.uid = player.uid    
    local ok,ret = R().activitysvr(1):call('.manager', 'get_reapack_award', msg)
    print("REQ_ACTIVITY_RANK====================",ok,ret)
    if not ok or not ret then
        send_to_gateway('activity.RSP_REDPACK_AWARD',{result = error_code.RSP_FAIL})
        return
    end
    send_to_gateway('activity.RSP_REDPACK_AWARD',ret)

    return true
end

--请求红包状态
function activity_handler.REQ_REDPACK_STATE(msg)    
    msg.uid = player.uid
    msg.today_pay_money = player.today_pay_money or 0
    local ok,ret = R().activitysvr(1):call('.manager','get_redpack_state', msg)
    print("REQ_REDPACK_STATE====================",ok,ret)
    if not ok or not ret then
        send_to_gateway('activity.RSP_REDPACK_STATE',{result = error_code.RSP_FAIL})
        return
    end
    send_to_gateway('activity.RSP_REDPACK_STATE',ret)

    return true
end

--请求救援金信息
function activity_handler.REQ_RESCUE_CONF(msg)    
    msg.uid = player.uid
    local ok,ret = R().activitysvr(1):call('.manager','get_rescue_conf', msg)
    print("REQ_RESCUE_CONF====================",ok,ret)
    if not ok or not ret then
        send_to_gateway('activity.RSP_RESCUE_CONF',{result = error_code.RSP_FAIL})
        return true
    end
    send_to_gateway('activity.RSP_RESCUE_CONF',ret)

    return true
end





--------------------------------------------------------
handler.register_module_handlers('activity',activity_handler)

